package com.zoshine.zzg.service;

import com.zoshine.zzg.dao.*;
import com.zoshine.zzg.exception.AppException;
import com.zoshine.zzg.exception.ErrorMessage;
import com.zoshine.zzg.pojo.*;
import com.zoshine.zzg.pojo.pojostatus.InvoiceStatus;
import com.zoshine.zzg.pojo.pojostatus.OrderStatus;
import com.zoshine.zzg.pojo.pojostatus.ReleaseSlipStatus;
import com.zoshine.zzg.pojo.pojostatus.SettlementStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author 田沛凌
 * @time 2017/7/28 15:31
 */
@Service
@Transactional
public class ReleaseSlipService extends BaseService<ReleaseSlip>{
    private InvoiceDao invoiceDao;
    private InvoiceDetailDao invoiceDetailDao;
    private OrderSupplierDetailDao orderSupplierDetailDao;
    private RelationSupplierPrisonerDao relationSupplierPrisonerDao;
    private OrderPrisonerDao orderPrisonerDao;
    private  OrderPrisonerDetailDao orderPrisonerDetailDao;
    private ReleaseSlipDao releaseSlipDao;
    private ReleaseSlipDetailDao releaseSlipDetailDao;
    private PrisonerSettlementDao prisonerSettlementDao;
    private PrisonerInfoDao prisonerInfoDao;
    @Autowired
    public void setInvoiceDao(InvoiceDao invoiceDao) {
        this.invoiceDao = invoiceDao;
    }
    @Autowired
    public void setInvoiceDetailDao(InvoiceDetailDao invoiceDetailDao) {
        this.invoiceDetailDao = invoiceDetailDao;
    }
    @Autowired
    public void setOrderSupplierDetailDao(OrderSupplierDetailDao orderSupplierDetailDao) {
        this.orderSupplierDetailDao = orderSupplierDetailDao;
    }
    @Autowired
    public void setRelationSupplierPrisonerDao(RelationSupplierPrisonerDao relationSupplierPrisonerDao) {
        this.relationSupplierPrisonerDao = relationSupplierPrisonerDao;
    }
    @Autowired
    public void setOrderPrisonerDao(OrderPrisonerDao orderPrisonerDao) {
        this.orderPrisonerDao = orderPrisonerDao;
    }
    @Autowired
    public void setOrderPrisonerDetailDao(OrderPrisonerDetailDao orderPrisonerDetailDao) {
        this.orderPrisonerDetailDao = orderPrisonerDetailDao;
    }
    @Autowired
    public void setReleaseSlipDao(ReleaseSlipDao releaseSlipDao) {
        this.releaseSlipDao = releaseSlipDao;
    }
    @Autowired
    public void setReleaseSlipDetailDao(ReleaseSlipDetailDao releaseSlipDetailDao) {
        this.releaseSlipDetailDao = releaseSlipDetailDao;
    }
    @Autowired
    public void setPrisonerSettlementDao(PrisonerSettlementDao prisonerSettlementDao) {
        this.prisonerSettlementDao = prisonerSettlementDao;
    }
    @Autowired
    public void setPrisonerInfoDao(PrisonerInfoDao prisonerInfoDao) {
        this.prisonerInfoDao = prisonerInfoDao;
    }

    /**
     * 根据收货单生成发放单
     */
    public void addReleaseSlip(){
        //状态为2，已收货的数据做处理
        Invoice inv=new Invoice();
        inv.setStatus(InvoiceStatus.FINISH.getValue());
        List<Invoice> invoices = invoiceDao.selectList(inv);
        for(Invoice invoiceTemp:invoices){
            Invoice invoice = invoiceDao.selectByPrimaryKey(invoiceTemp.getId());
            InvoiceDetail invoiceDetail=new InvoiceDetail();
            invoiceDetail.setInvoiceId(invoiceTemp.getId());
            List<InvoiceDetail> invoiceDetails = invoiceDetailDao.selectList(invoiceDetail);
            //获取非重复的订单号
            Set<Long> orderSupplierIds=new HashSet<>();
            for (InvoiceDetail invoiceDetail1:invoiceDetails){
                OrderSupplierDetail orderSupplierDetail = orderSupplierDetailDao.selectByPrimaryKey(invoiceDetail1.getOrderSupplierDetailId());
                orderSupplierIds.add(orderSupplierDetail.getOrderSupplierId());
            }
            //查询关系表获取点单信息Id集合
            List<Long> orderPrisonerIds=new ArrayList<>();
            for (Long idStr:orderSupplierIds){
                List<RelationSupplierPrisoner> relationSupplierPrisoners = relationSupplierPrisonerDao.selectByOrderSupplierId(idStr);
                for (RelationSupplierPrisoner relationSupplierPrisoner:relationSupplierPrisoners){
                    orderPrisonerIds.add(relationSupplierPrisoner.getOrderPrisonerId());
                }
            }
            //根据时间排序获取点单信息
            List<OrderPrisoner> orderPrisoners = orderPrisonerDao.selectByIds(orderPrisonerIds);

            //生成发放单
            ReleaseSlip releaseSlip=new ReleaseSlip();
            releaseSlip.setId(baseIdWorker.createId());
            releaseSlip.setAccountPrisons(invoice.getAccountPrisons());
            releaseSlip.setAccountSupplier(invoice.getAccountSupplier());
            releaseSlip.setInvoiceId(invoice.getId());
            releaseSlip.setCreateTime(new Date());
            releaseSlip.setUpdateTime(new Date());
            releaseSlip.setCreateUser(invoiceTemp.getCreateUser());
            releaseSlip.setStatus(ReleaseSlipStatus.PENDING_RELEASE.getValue());
            //生成发放单详情
            List<ReleaseSlipDetail> releaseSlipDetails=new ArrayList<>();
            for(InvoiceDetail temp:invoiceDetails){
                Integer surplus=temp.getCount();
                for (OrderPrisoner orderPrisoner:orderPrisoners){
                List<OrderPrisonerDetail> orderPrisonerDetails = orderPrisonerDetailDao.selectByOrderPrisonerId(orderPrisoner.getId());
                //某个商品的总数量
                    for(OrderPrisonerDetail orderPrisonerDetail:orderPrisonerDetails){
                        //如果已经收货完成的点单详情就不执行此操作
                        if(orderPrisonerDetail.getInvoiceCount()>=orderPrisonerDetail.getGoodsCount()){
                            continue;
                        }
                        if(orderPrisonerDetail.getGoodsId().equals(temp.getPublishDetailId())){
                            ReleaseSlipDetail releaseSlipDetail=new ReleaseSlipDetail();
                            releaseSlipDetail.setId(baseIdWorker.createId());
                            releaseSlipDetail.setReleaseSlipId(releaseSlip.getId());
                            releaseSlipDetail.setAccountPrisons(temp.getAccountPrisons());
                            releaseSlipDetail.setAccountSupplier(temp.getAccountSupplier());
                            releaseSlipDetail.setOrderPrisonerDetailId(orderPrisonerDetail.getId());
                            releaseSlipDetail.setPublishDetailId(temp.getPublishDetailId());
                            releaseSlipDetail.setGoodsHistoryId(temp.getGoodsHistoryId());
                            releaseSlipDetail.setArea(orderPrisoner.getArea());
                            releaseSlipDetail.setSubArea(orderPrisoner.getSubArea());
                            releaseSlipDetail.setPrisonerId(orderPrisoner.getPrisonerInfo());
                            //还需发货数量
                            Integer sum=orderPrisonerDetail.getGoodsCount()-orderPrisonerDetail.getInvoiceCount();
                            //如果发货数量大于点单数量
                            if(surplus >= sum){
                                //设置发放单详情数量，保存发放单详情
                                releaseSlipDetail.setCount(sum);
                                releaseSlipDetails.add(releaseSlipDetail);
                                //修改收货数量
                                orderPrisonerDetail.setInvoiceCount(orderPrisonerDetail.getInvoiceCount()+sum);
                                orderPrisonerDetailDao.update(orderPrisonerDetail);
                                //剩余数量减少
                                surplus-=sum;
                                continue;
                            }
                            //如果发货单数量小于点单详情数量
                            if(surplus < sum && surplus > 0){
                                //设置发放单详情数量，保存发放单详情。
                                releaseSlipDetail.setCount(surplus);
                                releaseSlipDetails.add(releaseSlipDetail);
                                //修改收货数量
                                orderPrisonerDetail.setInvoiceCount(orderPrisonerDetail.getInvoiceCount()+surplus);
                                orderPrisonerDetailDao.update(orderPrisonerDetail);
                                //剩余数量变为0
                                surplus=0;
                            }

                        }
                    }
                }
            }
            //插入数据
            if(releaseSlipDetails.size()>0){
                releaseSlipDao.insert(releaseSlip);
                releaseSlipDetailDao.batchInsert(releaseSlipDetails);
            }
        }
        //更新发货单状态为3，已生成发放单
        for (Invoice invoice:invoices){
            invoice.setStatus(InvoiceStatus.RELEASE.getValue());
            invoiceDao.update(invoice);
        }
        //更新犯人点单的收货数量
        ReleaseSlip releaseSlip=new ReleaseSlip();
        releaseSlip.setStatus(ReleaseSlipStatus.PENDING_RELEASE.getValue());
        List<ReleaseSlip> releaseSlips = releaseSlipDao.selectList(releaseSlip);

        //更新发放单状态
        releaseSlip.setStatus(ReleaseSlipStatus.FINISH.getValue());
        releaseSlipDao.update(releaseSlip);

        printReleaseSlip(releaseSlips);
    }

    /**
     * 打印发放单
     * @param releaseSlips 发放单集合
     */
    public void printReleaseSlip(List<ReleaseSlip> releaseSlips){
        Set<OrderPrisoner> orderPrisoners =new HashSet<>();
        for(ReleaseSlip releaseSlip:releaseSlips){
            ReleaseSlipDetail temp=new ReleaseSlipDetail();
            temp.setReleaseSlipId(releaseSlip.getId());
            List<ReleaseSlipDetail> releaseSlipDetails = releaseSlipDetailDao.selectList(temp);
                for (ReleaseSlipDetail releaseSlipDetail:releaseSlipDetails){
                    OrderPrisonerDetail orderPrisonerDetail = orderPrisonerDetailDao.selectByPrimaryKey(releaseSlipDetail.getOrderPrisonerDetailId());
                    if(orderPrisonerDetail == null){
                        throw new AppException(ErrorMessage.MODEL_CAN_NOT_FOUND);
                    }
                    //更新点单详情收货数量
                    //orderPrisonerDetail.setInvoiceCount(orderPrisonerDetail.getInvoiceCount()+releaseSlipDetail.getCount());
                    //orderPrisonerDetailDao.update(orderPrisonerDetail);
                    OrderPrisoner orderPrisoner = orderPrisonerDao.selectByPrimaryKey(orderPrisonerDetail.getOrderPrisonerId());
                    orderPrisoners.add(orderPrisoner);
                }
            //修改发货单状态
            releaseSlip.setStatus(ReleaseSlipStatus.FINISH.getValue());
            releaseSlipDao.update(releaseSlip);
        }
        //修改点单状态
        for(OrderPrisoner orderPrisoner:orderPrisoners){
            Integer totalInvoice=0;
                List<OrderPrisonerDetail> orderPrisonerDetails = orderPrisonerDetailDao.selectByOrderPrisonerId(orderPrisoner.getId());
                //三种状态，1，未收到货，状态不变。2，部分收货，状态为部分收货。3，全部收货，已完成状态
               // Integer status = 0;
                //Integer tempStatus=0;
            //    boolean flg=true;
                    for(OrderPrisonerDetail orderPrisonerDetail:orderPrisonerDetails){
                        totalInvoice+=orderPrisonerDetail.getInvoiceCount();
//                    if(flg){
//                        //如果收货数量等于总数量，则全部收货
//                        if(orderPrisonerDetail.getInvoiceCount().equals(orderPrisonerDetail.getGoodsCount())){
//                            status= OrderStatus.FINISHED.getValue();
//                            tempStatus = OrderStatus.PART_FINISH.getValue();
//                        }
//                        //如果有一个详情部分完成，则点单部分完成
//                        if(orderPrisonerDetail.getInvoiceCount()<orderPrisonerDetail.getGoodsCount() &&
//                                orderPrisonerDetail.getInvoiceCount()!=0){
//                            status=OrderStatus.PART_FINISH.getValue();
//                            flg=false;
//                        }
//                        //如果全部收货数量都为0，则状态依然为配送中
//                        if(orderPrisonerDetail.getInvoiceCount()==0){
//                            status=OrderStatus.INDELIVERING.getValue();
//                            if (!tempStatus.equals(0)){
//                                status=tempStatus;
//                            }
//                        }
//                    }
                    }
//
//            switch (status){
//                //4,表示依然为配送中，点单状态不变
//                case 4:
//                    break ;
//                //5，表示部分完成
//                case 5:
//                    orderPrisoner.setStatus(status);
//                    orderPrisoner.setUpdateTime(new Date());
//                    orderPrisoner.setTotalInvoice(totalInvoice);
//                    orderPrisonerDao.update(orderPrisoner);
//                    break;
//                //9，表示全部完成
//                case 9:
//                    orderPrisoner.setStatus(status);
//                    orderPrisoner.setUpdateTime(new Date());
//                    orderPrisoner.setTotalInvoice(totalInvoice);
//                    orderPrisonerDao.update(orderPrisoner);
//                    break;
//            }
            if (totalInvoice > 0 && totalInvoice < orderPrisoner.getTotalCount()){
                    orderPrisoner.setStatus(OrderStatus.PART_FINISH.getValue());
                    orderPrisoner.setUpdateTime(new Date());
                    orderPrisoner.setTotalInvoice(totalInvoice);
                    orderPrisonerDao.update(orderPrisoner);
            }
            if (totalInvoice.equals(orderPrisoner.getTotalCount())){
                orderPrisoner.setStatus(OrderStatus.FINISHED.getValue());
                orderPrisoner.setUpdateTime(new Date());
                orderPrisoner.setTotalInvoice(totalInvoice);
                orderPrisonerDao.update(orderPrisoner);
            }
            if (totalInvoice==0){
                orderPrisoner.setUpdateTime(new Date());
                orderPrisonerDao.update(orderPrisoner);
            }
        }
    }
}
